﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cosmetic.Logic;
using System.Configuration;
using System.Data;
using Cosmetic.DataEntity;

namespace WebApp.Controls
{
    public partial class UserBrowseHistory_Right : System.Web.UI.UserControl
    {

        private CategoryManager m_CategoryManager = new CategoryManager();
        private ProductManager m_ProductManager = new ProductManager();

        public int PointCoefficient = Convert.ToInt32(ConfigurationManager.AppSettings["PointCoefficient"]);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProduct();

            }
        }

        private void BindProduct()
        {
            string userName = "ViewHistory"; //((User)Session["SignInUser"]).UserName;
            HttpCookie readCookie = null; //Response.Cookies[userName]; //根据用户登陆的ID信息，读取Cookie的信息。
            for (int i = 0; i < Request.Cookies.Count; i++)
            {
                if (Request.Cookies[i].Name == userName)
                {
                    readCookie = Request.Cookies[userName];
                    break;
                }
            }

            DataTable dt = new DataTable();
            dt.Columns.Add("ProductId", typeof(System.String));
            dt.Columns.Add("PictureUrl", typeof(System.String));
            if (readCookie == null)
            {
                return;
            }

            if (!string.IsNullOrEmpty(readCookie.Value))
            {
                string cookievalue = readCookie.Value;
                string[] pictureUrl = cookievalue.Split(new char[] { '|' });
                //这里约定，分离出来的字符串中，用=号分开，前面的是“ProductId”，后面的是“PictureUrl”。
                if (pictureUrl.Count() < 6)
                {
                    for (int i = 0; i < pictureUrl.Count(); i++)
                    {
                        DataRow dr = dt.NewRow();
                        string theUrl = pictureUrl[i].ToString();
                        dr[0] = theUrl.Substring(0, theUrl.IndexOf('='));
                        dr[1] = theUrl.Substring(theUrl.IndexOf('=') + 1, theUrl.Length - theUrl.IndexOf('=') - 1);
                        dt.Rows.Add(dr);
                    }
                }
                else
                {
                    for (int i = 0; i < 6; i++)
                    {
                        DataRow dr = dt.NewRow();
                        string theUrl = pictureUrl[i].ToString();
                        dr[0] = theUrl.Substring(0, theUrl.IndexOf('='));
                        dr[1] = theUrl.Substring(theUrl.IndexOf('=') + 1, theUrl.Length - theUrl.IndexOf('=') - 1);
                        dt.Rows.Add(dr);
                    }
                }
            }
            this.lvProduct.DataSource = dt;
            this.lvProduct.DataBind();
        }
    }//end class
}